{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">原文地址 [mp.weixin.qq.com](https://mp.weixin.qq.com/s?__biz=MzU1Nzg4NjgyMw==&mid=2247483776&idx=1&sn=736fb79ad59257d1f1e83394ca27e711&chksm=fc2fbd88cb58349ef2134a3bd27e247ae755e619ca3334fbccb3054b47d969e843bbe7ec974b&scene=178&cur_album_id=1347208014788526081#rd)\n",
    "\n",
    "> 平时经常使用 Docker 来搭建各种环境，简单又好用！但是有时候往往会忘记命令，总结了一套非常实用的 Docker 命令，对于 Java 开发来说基本上够用了，希望对大家有所帮助！\n",
    "\n",
    "Docker 简介\n",
    "---------\n",
    "\n",
    "Docker 是一个开源的应用容器引擎，让开发者可以打包应用及依赖包到一个可移植的镜像中，然后发布到任何流行的 Linux 或 Windows 机器上。使用 Docker 可以更方便地打包、测试以及部署应用程序。\n",
    "\n",
    "Docker 环境安装\n",
    "-----------\n",
    "\n",
    "*   安装`yum-utils`；\n",
    "    \n",
    "\n",
    "```\n",
    "yum install -y yum-utils device-mapper-persistent-data lvm2\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "*   为 yum 源添加 docker 仓库位置；\n",
    "    \n",
    "\n",
    "```\n",
    "yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "*   安装 docker 服务；\n",
    "    \n",
    "\n",
    "```\n",
    "yum install docker-ce\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "*   启动 docker 服务。\n",
    "    \n",
    "\n",
    "```\n",
    "systemctl start docker\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "Docker 镜像常用命令\n",
    "-------------\n",
    "\n",
    "### 搜索镜像\n",
    "\n",
    "```\n",
    "docker search java\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNMbeXicmWDFccAibvNFPbDuX0ht6iaz9eM6mmia2lYCeowjfZAJzAbq0N7g/640?wx_fmt=png)\n",
    "\n",
    "### 下载镜像\n",
    "\n",
    "```\n",
    "docker pull java:8\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### 查看镜像版本\n",
    "\n",
    "> 由于`docker search`命令只能查找出是否有该镜像，不能找到该镜像支持的版本，所以我们需要通过`Docker Hub`来搜索支持的版本。\n",
    "\n",
    "*   进入`Docker Hub`的官网，地址：https://hub.docker.com\n",
    "    \n",
    "*   然后搜索需要的镜像：\n",
    "    \n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvN2Ga1MJBVQ6k2Zib8Jsv46hMOIYBkcGk9A4yTpDWibSVjDwmKOFF7vBgQ/640?wx_fmt=png)\n",
    "\n",
    "*   查看镜像支持的版本：\n",
    "    \n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNDY6905oa1MUibEnZYAAy7IeLlFj12vP0ibJibibic2aMg5A9vxibJJtaJsuA/640?wx_fmt=png)\n",
    "\n",
    "*   进行镜像的下载操作：\n",
    "    \n",
    "\n",
    "```\n",
    "docker pull nginx:1.17.0\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### 列出镜像\n",
    "\n",
    "```\n",
    "docker images\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvN6Z7dPGhTY3WY87PwXTaXtoI1vzYoFKdszWv4d8siawR7zQSFAt9cKwg/640?wx_fmt=png)\n",
    "\n",
    "### 删除镜像\n",
    "\n",
    "*   指定名称删除镜像：\n",
    "    \n",
    "\n",
    "```\n",
    "docker rmi java:8\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "*   指定名称删除镜像（强制）：\n",
    "    \n",
    "\n",
    "```\n",
    "docker rmi -f java:8\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "*   删除所有没有引用的镜像：\n",
    "    \n",
    "\n",
    "```\n",
    "docker rmi \\`docker images | grep none | awk '{print $3}'\\`\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "*   强制删除所有镜像：\n",
    "    \n",
    "\n",
    "```\n",
    "docker rmi -f $(docker images)\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### 打包镜像\n",
    "\n",
    "```\n",
    "# -t 表示指定镜像仓库名称/镜像名称:镜像标签 .表示使用当前目录下的Dockerfile文件\n",
    "docker build -t mall/mall-admin:1.0-SNAPSHOT .\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "Docker 容器常用命令\n",
    "-------------\n",
    "\n",
    "### ❤️新建并启动容器\n",
    "\n",
    "```\n",
    "docker run -p 80:80 --name nginx \\\\\n",
    "-e TZ=\"Asia/Shanghai\" \\\\\n",
    "-v /mydata/nginx/html:/usr/share/nginx/html \\\\\n",
    "-d nginx:1.17.0\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "*   \\-p：将宿主机和容器端口进行映射，格式为：宿主机端口: 容器端口；\n",
    "    \n",
    "*   \\--name：指定容器名称，之后可以通过容器名称来操作容器；\n",
    "    \n",
    "*   \\-e：设置容器的环境变量，这里设置的是时区；\n",
    "    \n",
    "*   \\-v：将宿主机上的文件挂载到宿主机上，格式为：宿主机文件目录: 容器文件目录；\n",
    "    \n",
    "*   \\-d：表示容器以后台方式运行。\n",
    "    \n",
    "\n",
    "### ❤️列出容器\n",
    "\n",
    "*   列出运行中的容器：\n",
    "    \n",
    "\n",
    "```\n",
    "docker ps\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNib8ub8SpPvN0g1cKSpw60rOBvUphqK7BG2vVcFeY5385dNwpCDLE4BQ/640?wx_fmt=png)\n",
    "\n",
    "*   列出所有容器：\n",
    "    \n",
    "\n",
    "```\n",
    "docker ps -a\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNMRK1hA4u3EEHPlrMbiaAK1mHEeSrMB6eVC36jCjqnnreZ8DzbuAxY4Q/640?wx_fmt=png)\n",
    "\n",
    "### 停止容器\n",
    "\n",
    "注意：`$ContainerName`表示容器名称，`$ContainerId`表示容器 ID，可以使用容器名称的命令，基本也支持使用容器 ID，比如下面的停止容器命令。\n",
    "\n",
    "```\n",
    "docker stop $ContainerName(or $ContainerId)\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "例如：\n",
    "\n",
    "```\n",
    "docker stop nginx\n",
    "#或者\n",
    "docker stop c5f5d5125587\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### 强制停止容器\n",
    "\n",
    "```\n",
    "docker kill $ContainerName\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### 启动容器\n",
    "\n",
    "```\n",
    "docker start $ContainerName\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### ❤️进入容器\n",
    "\n",
    "*   先查询出容器的`pid`：\n",
    "    \n",
    "\n",
    "```\n",
    "docker inspect --format \"{{.State.Pid}}\" $ContainerName\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "*   根据容器的 pid 进入容器：\n",
    "    \n",
    "\n",
    "```\n",
    "nsenter --target \"$pid\" --mount --uts --ipc --net --pid\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNjnLuj5JH1o74UVuJGqwCVQPscYXpJrf2ibSowehOkOYXyxwsYFTIBwA/640?wx_fmt=png)\n",
    "\n",
    "### 删除容器\n",
    "\n",
    "*   删除指定容器：\n",
    "    \n",
    "\n",
    "```\n",
    "docker rm $ContainerName\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "*   按名称通配符删除容器，比如删除以名称`mall-`开头的容器：\n",
    "    \n",
    "\n",
    "```\n",
    "docker rm \\`docker ps -a | grep mall-\\* | awk '{print $1}'\\`\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "*   强制删除所有容器；\n",
    "    \n",
    "\n",
    "```\n",
    "docker rm -f $(docker ps -a -q)\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### ❤️查看容器的日志\n",
    "\n",
    "*   查看容器产生的全部日志：\n",
    "    \n",
    "\n",
    "```\n",
    "docker logs $ContainerName\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNVN4MiatVIZUkhhPj75Iuub7dQxd7FSXCmBmKZLibPxNw3LYhyfpg8TcQ/640?wx_fmt=png)\n",
    "\n",
    "*   动态查看容器产生的日志：\n",
    "    \n",
    "\n",
    "```\n",
    "docker logs -f $ContainerName\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### 查看容器的 IP 地址\n",
    "\n",
    "```\n",
    "docker inspect --format '{{ .NetworkSettings.IPAddress }}' $ContainerName\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNn8ibdE0otpc0WTszz7pgvGzuVic2ghFMhFUKmYyj55L0ibJOPia6MygVDQ/640?wx_fmt=png)\n",
    "\n",
    "### 修改容器的启动方式\n",
    "\n",
    "```\n",
    "# 将容器启动方式改为always\n",
    "docker container update --restart=always $ContainerName\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### 同步宿主机时间到容器\n",
    "\n",
    "```\n",
    "docker cp /etc/localtime $ContainerName:/etc/\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### 指定容器时区\n",
    "\n",
    "```\n",
    "docker run -p 80:80 --name nginx \\\\\n",
    "-e TZ=\"Asia/Shanghai\" \\\\\n",
    "-d nginx:1.17.0\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### 查看容器资源占用状况\n",
    "\n",
    "*   查看指定容器资源占用状况，比如 cpu、内存、网络、io 状态：\n",
    "    \n",
    "\n",
    "```\n",
    "docker stats $ContainerName\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNu4iaIpiaDnQ6IOHOFPc9O9I3icKmTMzzw3IwAHYiaC6HKPanLdbIhskrUQ/640?wx_fmt=png)\n",
    "\n",
    "*   查看所有容器资源占用情况：\n",
    "    \n",
    "\n",
    "```\n",
    "docker stats -a\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNichA4T0tKvIS4MfTRrxMb82RS5sssIdlXE3zOBjbGwfV09D64xJZiaiaA/640?wx_fmt=png)\n",
    "\n",
    "### 查看容器磁盘使用情况\n",
    "\n",
    "```\n",
    "docker system df\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNdTpj1RdOibXUh2AmYkYz1J7mVng9ERl6Bs7icVwx9hbcK5rlTbicgKPDw/640?wx_fmt=png)\n",
    "\n",
    "### ❤️执行容器内部命令\n",
    "\n",
    "```\n",
    "docker exec -it $ContainerName /bin/bash\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNgBTSyibZlfWYxjqnUoUDic6dibSEPYGT6Ahu5WNpIwN5SMKMIgrfrOEeA/640?wx_fmt=png)\n",
    "\n",
    "### 指定账号进入容器内部\n",
    "\n",
    "```\n",
    "# 使用root账号进入容器内部\n",
    "docker exec -it --user root $ContainerName /bin/bash\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### 查看所有网络\n",
    "\n",
    "```\n",
    "docker network ls\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "```\n",
    "\\[root@local-linux ~\\]# docker network ls\n",
    "NETWORK ID          NAME                     DRIVER              SCOPE\n",
    "59b309a5c12f        bridge                   bridge              local\n",
    "ef34fe69992b        host                     host                local\n",
    "a65be030c632        none     \n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### 创建外部网络\n",
    "\n",
    "```\n",
    "docker network create -d bridge my-bridge-network\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "### 指定容器网络\n",
    "\n",
    "```\n",
    "docker run -p 80:80 --name nginx \\\\\n",
    "--network my-bridge-network \\\\\n",
    "-d nginx:1.17.0\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "修改镜像的存放位置\n",
    "---------\n",
    "\n",
    "*   查看 Docker 镜像的存放位置：\n",
    "    \n",
    "\n",
    "```\n",
    "docker info | grep \"Docker Root Dir\"\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNU8Hiarbbs7vEhrYtlJJGZIZa9ZYvfXXlh7K4xYIy4ouBaiaOGRLfEBiaw/640?wx_fmt=png)\n",
    "\n",
    "*   关闭 Docker 服务：\n",
    "    \n",
    "\n",
    "```\n",
    "systemctl stop docker\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "*   先将原镜像目录移动到目标目录：\n",
    "    \n",
    "\n",
    "```\n",
    "mv /var/lib/docker /mydata/docker\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "*   建立软连接：\n",
    "    \n",
    "\n",
    "```\n",
    "ln -s /mydata/docker /var/lib/docker\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNjPtnL1ZUHSt6l0QTHU8vv6wMcHh4h1t5fhY4FU8SSA1CMKJaLuGdwQ/640?wx_fmt=png)\n",
    "\n",
    "*   再次查看可以发现镜像存放位置已经更改。\n",
    "    \n",
    "\n",
    "![](https://mmbiz.qpic.cn/mmbiz_png/CKvMdchsUwl3E8cVETIzWWyuK3KGwGvNrr8IcAn7HVbm3968WeDP5RlORObiaZzJajw84bNVPiaf9Okv9QZoHUlQ/640?wx_fmt=png)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "288px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
